//
//  FansMangerTableViewController.m
//  1243
//
//  Created by 杨森 on 16/4/7.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "FansManagerTableViewController.h"
#import "FansManageTableViewCell.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"

@interface FansManagerTableViewController ()<UITableViewDelegate,UITableViewDataSource, UIAlertViewDelegate>

@property(nonatomic,strong)UITableView *FansManageTableView;

@property (nonatomic, strong) NSMutableArray *dataArrM;

@property(nonatomic,strong)UIView *seachView;

@property (nonatomic, strong) UITextField *searchText;

@end

@implementation FansManagerTableViewController

- (NSMutableArray *)dataArrM {
    
    if (!_dataArrM) {
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"粉丝管理";
    
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    [self addFansUI];
    [self loadData];
}

- (void)loadData {
    
    NSDictionary *patams = [ReadLoginData readLoginData];
    
    NSDictionary *dict = @{
                           @"UserId":[patams objectForKey:@"Number"]
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"GETMYFANSLIST" completeBlock:^(id object) {
        
        NSLog(@"%@", object);
        
        NSString *string = [object objectForKey:@"BackData"];
        NSArray *array =[NetWorkTools arrayWithJsonString:string];
        [self.dataArrM addObjectsFromArray:array];
        
        if ([object[@"BackData"] isEqualToString:@"[]"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无粉丝" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10001;
            
            [alert show];
        }
        
        [self.FansManageTableView reloadData];
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10001) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)addFansUI
{
    UIView *seachView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, WEITH,60)];
    seachView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    self.seachView = seachView;
    [self.view addSubview:seachView];
    
    UITextField *searchText = [[UITextField alloc] initWithFrame:CGRectMake(10,10, WEITH-20, 40)];
    searchText.placeholder = @"请您输入手机号码";
    searchText.layer.borderWidth = 1;
    searchText.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    searchText.backgroundColor = [UIColor clearColor];
    searchText.font = [UIFont systemFontOfSize:15];
    [seachView addSubview:searchText];
    self.searchText = searchText;
    [self setLeftViewWithTextField:searchText imageName:@"nav_2"];
    
    self.FansManageTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(seachView.frame),WEITH -20, HEIGHT - 124) style:UITableViewStyleGrouped];
    self.FansManageTableView.delegate = self;
    self.FansManageTableView.dataSource = self;
    self.FansManageTableView.rowHeight = 200;
    self.FansManageTableView.showsVerticalScrollIndicator = NO;
    self.FansManageTableView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    [self.view addSubview:self.FansManageTableView];

    NSLog(@"%@",self.view.subviews);


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.searchText resignFirstResponder];
}

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.backgroundColor = [UIColor clearColor];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"cell";
    
    FansManageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"FansManageView" owner:nil options:nil] lastObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic = self.dataArrM[indexPath.section];
    cell.number.text = [NSString stringWithFormat:@"编号:%@", dic[@"Number"]];
    cell.userName.text = [NSString stringWithFormat:@"手机号:%@", dic[@"UserName"]];
    cell.realName.text = [NSString stringWithFormat:@"姓名:%@", dic[@"RealName"]];
    NSString *lastLoginTime = [dic[@"LastLoginTime"] stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    cell.lastLoginTime.text = [NSString stringWithFormat:@"最近登录:%@", lastLoginTime];
    NSString *createTime = [dic[@"CreateTime"] stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    cell.createTime.text = [NSString stringWithFormat:@"注册日期:%@", createTime];
    cell.status.text = [NSString stringWithFormat:@"状态:%@", [dic[@"Status"] integerValue] == 1 ? @"已激活" : @"未激活"];
    
    return cell;
}

#pragma mark代理
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.searchText resignFirstResponder];
    
}

#pragma mark滚滚滚
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 0) {
        self.seachView.hidden = YES;
        self.FansManageTableView.frame = CGRectMake(10, 0, WEITH-20,HEIGHT);
    }else{
        self.seachView.hidden = NO;
        self.FansManageTableView.frame = CGRectMake(10,CGRectGetMaxY(self.seachView.frame), WEITH-20,HEIGHT);
    }
   
}


@end
